package Main;

import java.awt.event.*;
import AI.*;

public class MouseAction extends MouseAdapter{
	
	public MouseAction(Pieces p,BoardPanel bP, Rule r, AI a) {
		pieces = p;
		bPanel = bP;
		rule = r;
		evaluate = new CEvaluate();
		ai = a;
	}
	
	public void mouseClicked(MouseEvent e) {
		int x = e.getX()-BoardPanel.leftX;
		int y = e.getY()-BoardPanel.leftY;
		
		int dx = Pieces.dimX-1;
		int dy = Pieces.dimY-1;
		
		int gx = BoardPanel.gridW;
		int gy = BoardPanel.gridH;
		
		if(x<0 || x>dx*gx || y<0 || y>dy*gy) {
			return;
		}
		
		int ix = (x+gx/2)/gx;
		int iy = (y+gy/2)/gy;

		if(pieces.getOnePiece(ix, iy)==Pieces.BLANK) {
			pieces.setOnePiece(ix, iy);
			if(rule.isWin()){
				bPanel.hasWon = true;
			
			}
//			else{
//				boolean white = true;
//				if(pieces.lastColor == -1){
//					white = false;
//				}
//				int result = evaluate.evaluate(pieces.getPieces(), white);
//				System.out.println(result);
//			}
			OneStep step = ai.getNextStep();
			pieces.setOnePiece(step.x, step.y);
			if(rule.isWin()){
				bPanel.hasWon = true;
			
			}
			bPanel.repaint();
		}
	}
	
	private CEvaluate evaluate;
	private Rule rule;
	private Pieces pieces;
	private BoardPanel bPanel;
	private AI ai;
}
